package com.news.m;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.Jsoup;

import com.campus.utils.Http;
import com.campus.utils.StreamUtils;

import android.content.res.AssetManager;

public class Net {

	/**
	 * 查课表
	 */
	
	public static String resp(AssetManager am, String xq, String xn)
			throws MalformedURLException, IOException {
		String xqd = "1";
		String xnd = "2013-2014";
		String urlkebiao = "http://218.195.32.11/(lt0wzs55y42iw124ayvqeq55)/xskbcx.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121603";
		String resp = post(
				"http://218.195.32.11/(lt0wzs55y42iw124ayvqeq55)/Default2.aspx",
				"__VIEWSTATE=dDwtMjEzNzcwMzMxNTs7Pvtubc5lBpdxzJkAVrAeUsbvOB6m&TextBox1=1205500121&TextBox2=1205500121&RadioButtonList1=%D1%A7%C9%FA&Button1=");
		// String ok = Jsoup.parse(get(urlkebiao)).select("table").toString();
		String ok = get(urlkebiao);
		String f = Jsoup.parse(ok).select("input[name$=__EVENTTARGET").first()
				.attr("value");
		// System.out.println(StreamUtils.Stream2String(new
		// FileInputStream("D://xiazai//test.txt")));
		// String s =
		// (post("http://218.195.32.11/(lt0wzs55y42iw124ayvqeq55)/xskbcx.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121603",
		// "__EVENTTARGET=xnd&__EVENTARGUMENT=&__VIEWSTATE="+StreamUtils.Stream2String(new
		// FileInputStream("D://xiazai//test.txt")) + "&xnd=" + xnd + "&xqd=" +
		// xqd));
		// System.out.println(s);
		String p = StreamUtils.Stream2String(am.open("test.txt")) + "&xnd="
				+ xn + "&xqd=" + xq;

		String s = (post(
				"http://218.195.32.11/(lt0wzs55y42iw124ayvqeq55)/xskbcx.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121603",
				p));
		return (Jsoup.parse(s).select("table#Table1").attr("border", "1")
				.toString());
		// System.out.println(Jsoup.parse(Http.post("http://218.195.32.11/(umfxau553le1srvzeoakwcvg)/xskbcx.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121603",StreamUtils.Stream2String(new
		// FileInputStream("G://Xiaomi//xx.txt")))).select("span.formbox"));
	}

	
	/**
	 * 查成绩 
	 * @param am 资源管理器，加载cookie信息
	 */
	public static String doSearchGrades(AssetManager am, String xq, String xn)
			throws FileNotFoundException, IOException {
		String resp = post(
				"http://218.195.32.11/(lt0wzs55y42iw124ayvqeq55)/Default2.aspx",
				"__VIEWSTATE=dDwtMjEzNzcwMzMxNTs7Pvtubc5lBpdxzJkAVrAeUsbvOB6m&TextBox1=1205500121&TextBox2=1205500121&RadioButtonList1=%D1%A7%C9%FA&Button1=");
		// String ok = Jsoup.parse(get(urlkebiao)).select("table").toString();
		String s = "http://218.195.32.11/(lt0wzs55y42iw124ayvqeq55)/xscjcx.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121605";
		String p = StreamUtils.Stream2String(am.open("xx.txt")) + "&ddlXN="
				+ xn + "&ddlXQ=" + xq;
		resp = post(s, p);
		resp = Jsoup.parse(resp).select("table.datelist").toString();

		return resp;

	}

	static String post(String url, String p) {
		try {
			return Http.post(url, p, "gb2312");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	static String get(String url) {
		try {
			return Http.get(url, "gb2312");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
